import 'package:flutter/material.dart';
import 'package:flutter_house_manager/pages/home/index.dart';
import 'package:flutter_house_manager/pages/mine/index.dart';
import 'package:flutter_house_manager/utils/Emitter.dart';
import 'package:flutter_house_manager/utils/TokenManager.dart';
import 'package:flutter_house_manager/utils/toast.dart';
import 'package:flutter_html/flutter_html.dart';

class TabsPage extends StatefulWidget {
  const TabsPage({super.key});

  @override
  State<TabsPage> createState() => _TabsPageState();
}

class _TabsPageState extends State<TabsPage> {
  int _currentIndex = 0;
  List _tabList = [
    {
      "label": "首页",
      "icon": "assets/tabs/home_default.png",
      "acctive_icon": "assets/tabs/home_active.png"
    },
    {
      "label": "我的",
      "icon": "assets/tabs/my_default.png",
      "acctive_icon": "assets/tabs/my_active.png"
    },
  ];
  @override
  void initState() {
    super.initState();
    tokenManager.init();
    registerEvent();
  }

  registerEvent() {
    emitter.on<LogoutEvent>().listen((event) {
      PromptAction.showToast('退出成功');
      // 跳转到登录页
      Navigator.pushNamed(context, "/login");
    });
  }

  List<BottomNavigationBarItem> getTabsBar() {
    List<BottomNavigationBarItem> tabsList = [];
    for (var i = 0; i < _tabList.length; i++) {
      tabsList.add(
        BottomNavigationBarItem(
          icon: Image.asset(
            _tabList[i]["icon"],
            width: 30,
          ),
          label: _tabList[i]["label"],
          activeIcon: Image.asset(
            _tabList[i]["acctive_icon"],
            width: 30,
          ),
        ),
      );
    }

    return tabsList;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: IndexedStack(
          index: _currentIndex,
          children: [
            const HomeView(),
            MineView(activeIndex: _currentIndex),
          ],
        ),
      ),
      bottomNavigationBar: BottomNavigationBar(
          currentIndex: _currentIndex,
          onTap: (index) {
            _currentIndex = index;
            setState(() {
              _currentIndex = index;
            });
          },
          items: getTabsBar()),
    );
  }
}
